from django.shortcuts import render,redirect
#导包
from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
#导入类视图
from django.views import View

from myapp.models import User,Goods,Category
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
#导入加密库
import hashlib
#导入图片库
#绘画库
from PIL import ImageDraw
#字体库
from PIL import ImageFont
#图片库
from PIL import Image
#随机库
import random
#文件流
import io

import requests

#导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

#导入原生sql模块
from django.db import connection

import jwt

#导入redis数据库
import redis

#导入时间模块
import time

#导入公共目录变量
from mydjango.settings import BASE_DIR

#导包
from django.db.models import Q,F

#导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid

#定义地址和端口
host = '127.0.0.1'
port = 6379

#建立redis连接
r = redis.Redis(host=host,port=port)

# 导入序列化对象
from myapp.myser import UserSer
from myapp.myser import GoodsSer
from myapp.myser import CategorySer

  # 商品分类接口
class CategoryInfo(APIView):
    def get(self,request):
        # 进行商品查询
        category = Category.objects.all()
        # 进行序列化操作
        cate_ser = CategorySer(category,many=True)
        # 进行返回
        return Response(cate_ser.data)

# #定义商品分类接口
# class ClassifyView(APIView):
#     def get(self, request):
#         # 进行商品查询
#         category = Category.objects.all()
#         # 进行序列化操作
#         cate_ser = CategorySer(category,many=True)
#         # 进行返回
#         return Response(cate_ser.data)
#
#     def post(self, request):
#         # 增加分类
#         catename = request.data.get("catename")
#         # 去重
#         classify = Category.objects.filter(cname=catename).first()
#         if classify:
#             res = {'code': 405, "msg": '分类已存在'}
#             return Response(res)
#
#         classify = Category(cname=catename)
#         classify.save()
#         res = {'code': 200, 'msg': "添加成功！"}
#         return Response(res)
#
#     def delete(self, request):
#         # 删除分类
#         id = request.data.get("id")
#         # 去重
#         classify = Category.objects.filter(id=id).first()
#         classify.delete()
#         res = {'code': 200, 'msg': "删除成功！"}
#         return Response(res)
#
# def post(self,request):
#     # 修改
#     cid = request.data.get("id")
#     catename = request.data.get('catename')
#     # 去重
#     classify = Category.objects.filter(id=cid).first()
#     classify.cname = catename
#     classify.save()
#     res = {'code': 200, 'msg': "修改成功！"}
#     return Response(res)


  # 商品入库接口
class AddGoods(APIView):
    def get(self,request):
        name = request.GET.get('name')
        desc = request.GET.get('desc')
        img = request.GET.get('img')
        video = request.GET.get('video')
        param = request.GET.get('param')
        cid = request.GET.gte('cid')
        price = request.GET.get('price')

        # 排重
        goods = Goods.objects.filter(name=name).first()
        if goods:
            res = {}
            res['code'] = 405
            res['message'] = '商品已经存在'
            return Response(res)
        # 进行入库
        goods = Goods(name=name,desc=desc,img=img,video=video,param=param,cid=cid,price=price)
        goods.save()

        res = {}
        res['code'] = 200
        res['message'] = '商品添加成功'
        return Response(res)


  # 商品信息接口
class GoodsList(APIView):
    def get(self,request):
        # 获取用户ID
        uid = request.GET.get('uid')
        # 读取数据库
        goods = Goods.objects.filter(id = int(uid)).first()
        # 进行序列化操作
        goods_ser = GoodsSer(goods,many=True)
        # 进行返回
        return Response(goods_ser.data)




